<template>
  <div class='container'>
    <el-card>
      <!-- 搜索 清空 新增按钮 -->
      <div slot="header" class="clearfix">
        <el-h3>6组_张喜</el-h3><br><br>
        <span>
          <b style="margin-right:10px">学科名称</b>
          </span>
          <el-input  style="width:200px" v-model="subjectName" :disabled="isLoading"></el-input>
          <el-button style="margin-left:10px" @click="clearData" :disabled="isLoading">清除</el-button>
          <el-button type="primary" @click="search" :disabled="isLoading">搜索</el-button>
        <el-button style="float: right;cursor:pointer" type="success" @click="addsubjects" :disabled="isLoading">
          <i class="el-icon-edit">新增学科</i>
        </el-button>
      </div>
      <!-- 搜索到的信息提示 -->
      <el-alert
        :title="titleCounts"
        type="info"
        show-icon
        class="info"
        :closable="false"
        >
      </el-alert>
      <!-- 表格 -->
      <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%">
      <el-table-column
      type="index"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="学科名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
        label="创建日期"
        width='190'>
        <template slot-scope="scope" >
          <span>{{scope.row.addDate | parseTimeByString}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="前台是否显示"
        width="150">
        <template slot-scope="scope">
          <span v-text="scope.row.isFrontDisplay=='1'?'是':'否'"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop="twoLevelDirectory"
        label="二级目录"
        width="150">
      </el-table-column>
      <el-table-column
        prop="tags"
        label="标签"
        width="150">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="题目数量"
        width="150">
      </el-table-column>
      <el-table-column
        label="操作"
        width="300">
        <template slot-scope="scope">
          <span style="color: #79bbff;margin-right:10px;cursor:pointer" @click="toDirectorys(scope.row.id, scope.row.subjectName)">学科分类</span>
          <span style="color: #79bbff;margin-right:10px;cursor:pointer"  @click="toTags(scope.row.id, scope.row.subjectName)">学科标签</span>
          <span style="color: #79bbff;margin-right:10px;cursor:pointer"
          @click="toOpenUpdata(scope.row.id,scope.row.subjectName,scope.row.isFrontDisplay)">修改</span>
          <span style="color: #79bbff;cursor:pointer" @click="toDelete(scope.row.id)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :disabled="isLoading"
      background
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15]"
      :page-size="pagesize"
      layout=" prev, pager, next, sizes, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 新增学科的弹窗 -->
    <el-dialog
      :title="dialogStatus == 'add'?'新增学科':'修改学科'"
      :visible.sync="addShow"
      v-if="addShow"
      width="30%"
      :before-close="handleClose">
      <div>
          <el-form :model="form" :rules="rules" ref="form">
             <el-form-item prop="newName" label="学科名称">
                <el-input
                  v-model="form.newName"
                  style="width:200px"
                  placeholder="请输入学科名称">
                </el-input>
            </el-form-item>
          </el-form>
       是否显示<el-switch v-model="isShow" active-color="#13ce66"></el-switch>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="toCancel">取 消</el-button>
        <el-button type="primary" @click="toSure" :disabled="isLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, remove, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'Subjects',
  components: {
  },
  data () {
    return {
      titleCounts: '', // 存放提示信息  多少条数据
      tableData: [], // 数据
      page: 1, // 当前页
      pagesize: 10, // 每页多少条
      subjectName: null, // 学科名
      subjectID: null, // 学科id
      total: 0, // 数据数量
      addShow: false, // 弹窗关闭
      form: {
        newName: '' // 修改学科名
      },
      rules: {
        newName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' } // input框表单验证
        ]
      },
      isShow: false, // 是否显示在前台
      dialogStatus: '', // 触发弹窗
      isLoading: false // 加载效果
    }
  },
  // 搜索
  created () {
    this.acquire()
  },
  methods: {

    // 搜索学科名对应数据
    search () {
      this.acquire()
      this.isLoading = true
    },
    // 清空input框
    clearData () {
      this.subjectName = null
      this.acquire()
    },
    // 点击页码
    handleCurrentChange (newPage) {
      this.page = newPage
      this.acquire()
    },
    // 每页条数变化
    handleSizeChange (pageCount) {
      this.pagesize = pageCount
      this.page = 1
      this.acquire()
    },
    // 删除学科
    toDelete (id) {
      this.$confirm('此操作将永远删除，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await remove({
            id
          })
          this.$message.success('删除成功')
          this.acquire()
        } catch (err) {
          this.$message.error('删除失败')
          console.log(err)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 打开新增或者修改弹窗
    async toSure () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.dialogStatus === 'add') {
            // 添加学科的操作
            await add({
              subjectName: this.form.newName,
              isFrontDisplay: this.isShow ? 1 : 0
            })
            this.$message.success('新增学科成功')
            this.isShow = false
          } else {
            // 修改学科的操作
            try {
              await update({
                id: this.form.id,
                subjectName: this.form.newName,
                isFrontDisplay: this.isShow ? 1 : 0
              })
              this.$message.success('修改学科成功')
              this.isShow = false
            } catch (error) {
              this.$message.error('修改学科失败')
            }
          }
          this.addShow = false
          this.form.newName = ''
          this.acquire()
        } else {
          this.$message.error(this.dialogStatus === 'add' ? '添加失败' : '修改失败')
          return false
        }
        console.log(this.$refs)
      })
    },
    //  打开修改弹窗
    toOpenUpdata (name, id, show) {
      this.form.newName = id
      this.dialogStatus = 'updata'
      this.addShow = true
      this.form.id = name
      if (show === 0) {
        this.isShow = false
      } else {
        this.isShow = true
      }
    },
    // 打开添加弹窗
    addsubjects () {
      this.addShow = true
      this.dialogStatus = 'add'
      this.isShow = true
    },
    // 关闭弹窗之后 需要进行的操作
    handleClose () {
      this.addShow = false
      this.form.newName = ''
      this.isShow = false
    },
    // 取消添加内容
    toCancel () {
      this.addShow = false
      this.form.newName = ''
      this.isShow = false
    },
    // 跳转分类
    toDirectorys (id, name) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id,
          name
        }
      })
    },
    // 获取数据
    async acquire () {
      this.isLoading = true
      const result = await list({
        page: this.page,
        pagesize: this.pagesize,
        subjectName: this.subjectName
      })
      console.log(result)
      this.tableData = result.data.items
      this.total = result.data.counts
      this.titleCounts = `数据一共有${this.total}条`
      this.isLoading = false
    },
    // 跳转标签
    toTags (id, name) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          id,
          name
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.container{
  padding: 20px;
}
.pagination{
  float: right;
}
.alert {
  margin: 10px 0px 10px 0px;
}
.IUP {
  margin: 0px 10px;
}
.pagination {
  margin-top: 10px;
  float: right;
}
</style>

<style>
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
</style>
